package org.harlov.testsys.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.harlov.testsys.loger;

public class themes {
	
	private static db_main DB;
	private static Vector<String> themes_list = new Vector<String>();
	
	public themes (db_main DB_m) {
		DB = DB_m;
	}
	
	public static Vector<String> getThemes () {
		
	themes_list.clear();
	ResultSet res;
	try {
		
		res = DB.makeReq("SELECT name FROM themes ORDER BY id");
		while (res.next()) {
			themes_list.add(res.getString("name"));
		}
		
		
	}
	catch (SQLException e ) {loger.add(e.getMessage(), null, 1);}
	return themes_list;
	}
	
	
	public static int addTheme(String name) {
		try {
			DB.makeUp("INSERT INTO themes (name) VALUES('"+name+"')");
		}
		catch (SQLException e) {loger.add("Error while add theme", null, 1);}
		return 0;
	}
	
	public static int getIdByList(int _id) {
		int ret = -1;
		ResultSet res;
		try {
			res = DB.makeReq("SELECT id FROM themes WHERE name='"+themes_list.get(_id)+"'");
			if (res.next()) ret = res.getInt("id");
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		System.out.println("Theme ID>"+ret);
		return ret;
	}
	
	public static int getListbyId (int _list) {
		ResultSet res;
		int cou = 0;
		int _id = 0;
		try {
			res = DB.makeReq("SELECT id FROM themes");		
			
			while (res.next()) {
				if (res.getInt("id") ==_list) _id = cou;
				cou++;
			}
			
		}
		catch (SQLException e) {loger.add("ERROR:getListById",null, 1);}
		return _id;
	}
	
	

}
